package com.baomidou.mybatisplus.samples.deluxe.service;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.samples.deluxe.entity.User;
import com.baomidou.mybatisplus.samples.deluxe.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @Description TODO
 * @Author benjamin
 * @Date 2020/1/17 9:33
 * @Version
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper mapper;


    @Override
    @Transactional
    public R<User> create(User user) {
        mapper.insert(user);
        return R.ok(user);
    }

    @Override
    @Transactional
    public R<List<User>> batchCreate(List<User> userList) {
        LinkedList<User> list = new LinkedList<>();
        userList.forEach(new Consumer<User>() {
            @Override
            public void accept(User user) {
                User data = create(user).getData();
                list.add(data);
            }
        });
        return R.ok(list);
    }
}
